package cn.autoOffice.service.impl;
/*
 *功能说明：讨论业务实现类
 *@author 皮永浪
 *@Date 2023/3/27 14:07
 */

import cn.autoOffice.bean.pojo.Discuss;
import cn.autoOffice.mapper.DiscussMapper;
import cn.autoOffice.service.DepartmentService;
import cn.autoOffice.service.DiscussService;
import cn.autoOffice.service.RedisService;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DiscussServiceImpl implements DiscussService {

    @Autowired
    @Resource
    private DiscussMapper discussMapper;

    @Autowired
    private RedisService redisService;

    public Integer countDiscuss(Integer titleId){
        String count = redisService.getValue("allDisCount");
        if (count != null && !"".equals(count)){
            return Integer.parseInt(count);
        } else {
            Integer integer = discussMapper.countDiscuss(titleId);
            redisService.putValue("allEmpCount",integer);
            return integer;
        }
    }

    public List<Discuss> getDiscussTitle(Integer empId){
        // String value = null;
        List<Discuss> discussList = null;
        // if (empId == null){
        //     value = redisService.getValue("discussAll");
        //     if (value == null || "".equals(value)){
        //         discussList = discussMapper.getDiscussTitle(empId);
        //         redisService.putValue("discussAll",discussList,60);
        //     } else {
        //         discussList = JSONObject.parseArray(value);
        //     }
        // } else {
        //     value = redisService.getValue("discuss" + empId);
        //     if (value == null || "".equals(value)){
                discussList = discussMapper.getDiscussTitle(empId);
        //         redisService.putValue("discuss" + empId,discussList,60);
        //     } else {
        //         discussList = JSONObject.parseArray(value);
        //     }
        // }
        // System.out.println("discussList = " + discussList);
        return discussList;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public Integer addDiscussByTitle(Discuss discuss){
        Integer integer = null;
        if (discuss.getTitleName() != null){
            integer = discussMapper.addDiscussByTitle(discuss);
            redisService.cleaCache();
        }
        return integer;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public Integer deleteDiscussByTitleId(Discuss discuss){
        Integer integer = discussMapper.deleteDiscussByTitleId(discuss);
        redisService.cleaCache();
        return integer;
    }

    public List<Discuss> getDiscussByOne(@Param("titleId") Integer titleId){
        return discussMapper.getDiscussByOne(titleId);
    }
}
